/*
moxl - Modular XML Library
Copyright (c) 2007, Malcolm Sparks

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package moxl.xpath.parser.types;

import java.util.ArrayList;
import java.util.List;

import moxl.dom.MoxlNode;
import moxl.xpath.XPathContext;
import moxl.xpath.XPathExpression;
import moxl.xpath.axes.InvalidAxisForNodeException;
import moxl.xpath.parser.Operand;

public class PathExpression implements XPathExpression, NonTerminal, Operand
{
	private final List<Step> steps = new ArrayList<Step>();

	public void addStep(Step step)
	{
		steps.add(step);
	}

	public List<Step> listSteps()
	{
		return steps;
	}

	public Step getStep(int ix)
	{
		return (Step) steps.get(ix);
	}

	public AbstractXPathValue evaluate(XPathContext context) throws XPathEvaluationException
	{
		MoxlNode ctxNode = context.getContextNode();
		List<MoxlNode> set = new ArrayList<MoxlNode>();
		set.add(ctxNode);
		for (Step step : steps)
		{
			try
			{
				set = step.run(set, context);
			}
			catch (InvalidAxisForNodeException e)
			{
				e.printStackTrace();
			}
		}
		XPathNodeSetValue result = new XPathNodeSetValue(set);
		return result;
	}
}
